package net.stogi.dsper.api.util;

import net.stogi.dsper.api.dto.DataSourceDto;
import net.stogi.dsper.api.dto.TagDto;
import net.stogi.dsper.api.dto.UserDto;
import net.stogi.dsper.api.dto.UserProfileDto;
import net.stogi.dsper.api.model.DataSource;
import net.stogi.dsper.api.model.Tag;
import net.stogi.dsper.api.model.User;
import net.stogi.dsper.api.model.UserProfile;

import org.springframework.beans.BeanUtils;

public class ModelToDtoConverterUtils {

	public static DataSourceDto convertDataSourceModelToDto(DataSource dataSource) {
		DataSourceDto dataSourceDto = new DataSourceDto();
		BeanUtils.copyProperties(dataSource, dataSourceDto);
		return dataSourceDto;
	}

	public static TagDto convertTagModelToDto(Tag tag) {
		TagDto tagDto = new TagDto();
		BeanUtils.copyProperties(tag, tagDto);
		return tagDto;
	}

	public static UserDto convertUserModelToDto(User user) {
		UserDto userDto = new UserDto();
		BeanUtils.copyProperties(user, userDto);
		return userDto;
	}

	public static UserProfileDto convertUserProfileModelToDto(UserProfile userProfile) {
		UserProfileDto userProfileDto = new UserProfileDto();

		BeanUtils.copyProperties(userProfile, userProfileDto, new String[] { "dataSources", "tags" });

		// Copy data source set.
		if (userProfile.getDataSources() != null) {
			for (DataSource dataSource : userProfile.getDataSources()) {
				userProfileDto.getDataSources().add(convertDataSourceModelToDto(dataSource));
			}
		}
		// Copy tag set.
		if (userProfile.getTags() != null) {
			for (Tag tag : userProfile.getTags()) {
				userProfileDto.getTags().add(convertTagModelToDto(tag));
			}
		}

		return userProfileDto;
	}

	private ModelToDtoConverterUtils() {}
}
